package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/09 23:03
 **/
public class Day24_4_Num19_RemoveNthFromEnd {
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            int size = 0;
            ListNode cur = head;
            while(cur != null){cur = cur.next; size++;}  // 统计节点数
            if(size - n == 0) return head.next;  // 删除首节点的情况
            cur = head;
            int count = size-n-1;
            while(count-- > 0) cur = cur.next;
            cur.next = cur.next.next;   // 删除中间节点
            return head;
        }
    }
}